/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import modele.Etudie;
import modele.NewHibernateUtil;
import modele.Personne;
import modele.Promotion;
import modele.Universite;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Aimee
 */
public class AjoutEtude extends ActionSupport implements SessionAware {

    private Map<String, Object> session;
    private Session hibernateSession;
    public String diplome;
    public String anneeDebut;
    public String anneeFin;
    public String nomUniversite;
    public String idPersonne;
    public List<Promotion> listePromotion;

    
    
    /**
     * @return the diplome
     */
    public String getDiplome() {
        return diplome;
    }

    /**
     * @param diplome the diplome to set
     */
    public void setDiplome(String diplome) {
        this.diplome = diplome;
    }

    /**
     * @return the anneeDebut
     */
    public String getAnneeDebut() {
        return anneeDebut;
    }

    /**
     * @param anneeDebut the anneeDebut to set
     */
    public void setAnneeDebut(String anneeDebut) {
        this.anneeDebut = anneeDebut;
    }

    /**
     * @return the anneeFin
     */
    public String getAnneeFin() {
        return anneeFin;
    }

    /**
     * @param anneeFin the anneeFin to set
     */
    public void setAnneeFin(String anneeFin) {
        this.anneeFin = anneeFin;
    }

    /**
     * @return the universite
     */
    public String getUniversite() {
        return nomUniversite;
    }

    /**
     * @param universite the universite to set
     */
    public void setUniversite(String nomUniversite) {
        this.nomUniversite = nomUniversite;
    }

    /**
     * @return the session
     */
    public Map<String, Object> getSession() {
        return session;
    }

    /**
     * @param session the session to set
     */
    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    /**
     * @return the hibernateSession
     */
    public Session getHibernateSession() {
        return hibernateSession;
    }

    /**
     * @param hibernateSession the hibernateSession to set
     */
    public void setHibernateSession(Session hibernateSession) {
        this.hibernateSession = hibernateSession;
    }

    /**
     * @return the idPersonne
     */
    public String getIdPersonne() {
        return idPersonne;
    }

    /**
     * @param idPersonne the idPersonne to set
     */
    public void setIdPersonne(String idPersonne) {
        this.idPersonne = idPersonne;
    }

    /**
     * @return the listePromotion
     */
    public List<Promotion> getListePromotion() {
        return listePromotion;
    }

    /**
     * @param listePromotion the listePromotion to set
     */
    public void setListePromotion(List<Promotion> listePromotion) {
        this.listePromotion = listePromotion;
    }
    
    public String ajoutEtude() throws Exception {
        setHibernateSession(NewHibernateUtil.getSessionFactory().openSession());
        getHibernateSession().beginTransaction();
        Personne pers = (Personne) hibernateSession.createQuery("from Personne where idPersonne =" + idPersonne).uniqueResult();
        String rep = "select nom from Universite as nom where nom.nomUniversite = '" + nomUniversite +"'";
        Query query = hibernateSession.createQuery(rep);
        Universite univ = (Universite) query.uniqueResult();
        Promotion promotion = (Promotion) hibernateSession.createQuery("from Promotion as promo where diplome= '" + diplome.toUpperCase() + "' and anneeDebut=" + anneeDebut + "and promo.universite.idUniversite = " + univ.getIdUniversite()).uniqueResult();
        if (promotion == null) {
            Promotion promo = new Promotion();
            promo.setAnneeDebut(Integer.valueOf(anneeDebut));
            promo.setAnneeFin(Integer.valueOf(anneeFin));
            promo.setDiplome(diplome.toUpperCase());
            promo.setUniversite(univ);
            getHibernateSession().save(promo);
            Etudie etud = new Etudie();
            etud.setPersonne(pers);
            etud.setPromotion(promo);
            getHibernateSession().save(etud);
            getHibernateSession().getTransaction().commit();
        } else {
            Etudie etud = new Etudie();
            etud.setPersonne(pers);
            etud.setPromotion(promotion);
            getHibernateSession().save(etud);
            getHibernateSession().getTransaction().commit();
        }


        listePromotion = (hibernateSession.createQuery("select promo from Etudie as promo where idPersonne= " + idPersonne).list());
        session.put("listePromotion", listePromotion);
        return "ok";
    }

}
